package com.bdqn.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdqn.crm.pojo.BasDict;
import com.bdqn.crm.pojo.Product;
import com.bdqn.crm.service.ProductService;
import com.bdqn.crm.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("product")
public class ProductController {

    @Resource
    private ProductService productService;

    @RequestMapping("list")
    public String list(HttpServletRequest request, Model model, @RequestParam(value = "prodName",required = false) String prodName, @RequestParam(value = "prodType",required = false) String prodType, @RequestParam(value = "prodBatch",required = false) String prodBatch, @RequestParam(required = false, defaultValue = "1") int pageIndex){
        Page<Product> page = new Page<>(pageIndex, 5);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        //判断是否有查询条件
        if (prodName != null && prodName.trim() != "" ) {
            queryWrapper.like("prod_name", prodName);      //模糊设置条件
        }
        if (prodType != null && prodType.trim() != "") {
            queryWrapper.like("prod_type", prodType);
        }
        if (prodBatch != null && prodBatch.trim() != "") {
            queryWrapper.like("prod_batch", prodBatch);
        }

        if (productService.count(queryWrapper) == 0) {     //此操作说明 数据库没有数据可展示，那么就全部清零
            page.setTotalElements(0);
            page.setContent(null);
        } else {
            page.setTotalElements(productService.count(queryWrapper));
            page.setContent(productService.selectPages(prodName,prodType, prodBatch,(page.getNumber() - 1) * page.getPageSize(), page.getPageSize()));
        }
        model.addAttribute("prodName",prodName);
        model.addAttribute("prodType",prodType);
        model.addAttribute("prodBatch",prodBatch);
        model.addAttribute("productPage", page);
        return "product/list";
    }



}
